﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace XunChi.Dapper
{
    /// <summary>
    ///     查看Dapper最终生成的SQL语句
    /// </summary>
    internal class DapperSQLFormatter
    {
        /// <summary>
        /// 索引
        /// </summary>
        public string Key { get; set; }
        /// <summary>
        /// 属性值
        /// </summary>
        public string Value { get; set; }

        /// <summary>
        /// 获取这条SQL语句
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        public static void GetSql(string sql, object parameters)
        {
            try
            {
                string tempSql = sql;
                if (parameters != null)
                {
                    string[] arr = parameters.ToString().Replace("{", "").Replace("}", "").Split(',');
                    List<DapperSQLFormatter> paramList = new List<DapperSQLFormatter>();
                    foreach (var item in arr)
                    {
                        DapperSQLFormatter kv = new DapperSQLFormatter();
                        string[] temp = item.Split('=');
                        kv.Key = temp[0].Trim();
                        kv.Value = temp[1].Trim();
                        paramList.Add(kv);
                    }

                    foreach (var par in paramList)
                    {
                        //tempSql = tempSql.Replace("@" + par.Key, "'" + par.Value + "'");
                        tempSql = tempSql.Replace(":" + par.Key, "'" + par.Value + "'");
                    }
                }

                Console.WriteLine(tempSql);
                //return tempSql;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"异常错误消息是：{ex.Message}");
            }
        }

        public static void GetSqlByParams(string sql, params object[] parameters)
        {
            try
            {
                string tempSql = sql;
                string[] arrryList = tempSql.Split(':');
                List<string> strList = new List<string>();
                for (var index = 0; index < arrryList.Length; index++)
                {
                    if (index==0)
                    {
                        continue; 
                    }
                    string[] arrryList2 = arrryList[index].Split(' ');
                    strList.Add(arrryList2[0]);
                }

                if (parameters != null)
                {
                    for (int index = 0; index < parameters.Length; index++)
                    {
                        object param = parameters[index];
                        
                        tempSql = tempSql.Replace(":" + strList[index], "'" + param.ToString() + "'");
                        //tempSql = tempSql.Replace("@" + par.Key, "'" + par.Value + "'");
                        
                    }
                }

                Console.WriteLine(tempSql);
                //return tempSql;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"异常错误消息是：{ex.Message}");
            }
        }

        public static void GetSqlByParams(string sql,string parametersValue)
        {
            try
            {
                string tempSql = sql;
                string[] arrryList = tempSql.Split(':');
                List<string> strList = new List<string>();
                for (var index = 0; index < arrryList.Length; index++)
                {
                    if (index == 0)
                    {
                        continue;
                    }
                    string[] arrryList2 = arrryList[index].Split(' ');
                    strList.Add(arrryList2[0]);
                }

                if (parametersValue != null)
                {
                    for (int index = 0; index < parametersValue.Length; index++)
                    {
                        object param = parametersValue[index];

                        tempSql = tempSql.Replace(":" + strList[index], "'" + param.ToString() + "'");
                        //tempSql = tempSql.Replace("@" + par.Key, "'" + par.Value + "'");

                    }
                }

                Console.WriteLine(tempSql);
                //return tempSql;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"异常错误消息是：{ex.Message}");
            }
        }


        /// <summary>
        ///     获取一个类指定的属性值
        /// </summary>
        /// <param name="info">object对象</param>
        /// <param name="field">属性名称</param>
        /// <returns></returns>
        public static object GetPropertyValue(object info, string field)
        {
            if (info == null) return null;
            Type t = info.GetType();
            IEnumerable<PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi;
            return property.First().GetValue(info, null);
        }
     
    }
}